const path = require("path");

const HTMLWebpackPlugin = require("html-webpack-plugin");



const config = require("./index");

const webpack = require('webpack');

let HTMLPlugins = [];

let Entries = config.getEntry('./src/pages/*/*.js');

var pages = config.getEntry('./src/pages/*/*.html');

for (var pathname in pages) {
    const htmlPlugin = new HTMLWebpackPlugin({
        filename: pathname + '.html',
        template: pages[pathname],
        inject: true,
        favicon:'',
        chunks:[pathname].concat(config.htmlChunks),
        hunksSortMode: 'dependency',
        minify:{
            removeComments:false, 
            collapseWhitespace:false
        }        
    });
    HTMLPlugins.push(htmlPlugin);
 } 

module.exports = {
    entry:Entries,
    devtool:config.sourceMap,
    output:{
        filename:"[name].bundle.[hash].js",
        path:path.resolve(__dirname,"../dist"),
        publicPath:'/',
        chunkFilename: "[chunkhash].js"        
    },
    resolve:{
        modules: [path.resolve(__dirname, "./src"), "node_modules"],
        extensions: ['.js', '.json', '.scss','.less','jsonp','.jsx'],
        alias:{
            '@':path.resolve(__dirname,'../static'),
            'bootstrapConfig':path.resolve(__dirname,'../bootstarpConfig'),
        },
        plugins:[]
    },    
    // 加载器
    module:{
        rules:[
            {
                test: require.resolve('jquery'),
                loader: 'expose-loader?$!expose-loader?jQuery'
            },
            {
                test: /\.html$/,
                use: {
                    loader: 'html-loader',
                    options: {
                      attrs: [':data-src','img:src'],
                      minimize: true
                    }
                }
            },                        
            { 
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                // include: path.join(__dirname, '/src'),
                loader: 'babel-loader'

            },                            
            {
                test: /\.(png|svg|jpg|gif)$/,
                use:{               
                    loader:'url-loader?limit=1&name=images/[hash:8].[name].[ext]'                   
                }
            },
            {
                test: /\.(woff|woff2|eot|ttf|otf)$/,
                use:["file-loader"]
            }
        ],
    },
    plugins:[  
        new webpack.ProvidePlugin({
            $: 'jquery',
            jQuery: 'jquery',
            'window.jQuery': 'jquery',
            'window.$': 'jquery',
        }),          
          ...HTMLPlugins            
    ],
}


